package com.letoken.platform.repo.wallet.redis;

import com.letoken.platform.pub.constant.RedisKeyConstant;
import com.letoken.platform.pub.util.BeanUtil;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 *
 * @version 1.0
 * @author： letokenlabs
 * @date： 2021/11/24 9:55
 * 扫块失败的列表
 */
public class BlockReplayFailListRedis {
    private StringRedisTemplate stringRedisTemplate;

    private BlockReplayFailListRedis() {
        stringRedisTemplate = BeanUtil.getBean(StringRedisTemplate.class);
    }

    private enum Singleton {
        INSTANCE;
        private BlockReplayFailListRedis instance;

        // JVM会保证此方法绝对只调用一次
        Singleton() {
            instance = new BlockReplayFailListRedis();
        }
    }

    String cacheKey = RedisKeyConstant.LETOKEN_USER_WALLET_BLACK;

    public static BlockReplayFailListRedis me() {
        return BlockReplayFailListRedis.Singleton.INSTANCE.instance;
    }

    public void push(String blockNumber) {
        if (blockNumber != null) {
            stringRedisTemplate.opsForList().leftPush(RedisKeyConstant.CEX_BLOCK_REPLAY_FAIL_LIST, blockNumber);
        }
    }

    public String getPopItem(){
        return stringRedisTemplate.opsForList().rightPop(RedisKeyConstant.CEX_BLOCK_REPLAY_FAIL_LIST);
    }

    /**
     * @param key ... 多个key
     * @return
     */
    public Long del(String... key) {
        return stringRedisTemplate.opsForHash().delete(cacheKey, key);
    }
}
